package com.cloud.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *describe: 自定义 zuulpost 过滤器
 *
 *@author zhanghaixuan
 *@date 2018/10/02
 **/
@Component
public class AddResponseFilter extends ZuulFilter {
	@Override
	public String filterType() {
		return FilterConstants.POST_TYPE;
	}

	@Override
	public int filterOrder() {
		return FilterConstants.SEND_RESPONSE_FILTER_ORDER -1;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() throws ZuulException {
		/**
		 * zuul定义的 RequestContext 可以获取到当前的请求
		 */
		RequestContext currentContext = RequestContext.getCurrentContext();
		HttpServletResponse response = currentContext.getResponse();

		response.setHeader("test",Long.toString(System.currentTimeMillis()) );

		return null;
	}
}
